package com.apex.sdk.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.apex.bluetooth.callback.WatchInfoCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleWatchInfo;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityWatchInfoBinding;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.Calendar;


public class WatchInfoActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                EABleWatchInfo eaBleWatchInfo = (EABleWatchInfo) msg.obj;
                activityWatchInfoBinding.watchId.setText(eaBleWatchInfo.getWatchId());
                activityWatchInfoBinding.watchType.setText(eaBleWatchInfo.getWatchType());
                activityWatchInfoBinding.firmwareVersion.setText(eaBleWatchInfo.getFirmwareVersion());
                activityWatchInfoBinding.bindingStatus.setText(eaBleWatchInfo.getBindingInfo() == EABleWatchInfo.BindingInfo.bound ? getString(R.string.bound) : getString(R.string.unbound));
                activityWatchInfoBinding.userId.setText(eaBleWatchInfo.getUserId());
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(eaBleWatchInfo.getAgps_update_timestamp() * 1000);
                activityWatchInfoBinding.agpsUpdateTime.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE));
                activityWatchInfoBinding.blueAddress.setText(eaBleWatchInfo.getBle_mac_addr());
                activityWatchInfoBinding.requiredBind.setText(eaBleWatchInfo.getIs_wait_for_binding() == 0 ? getString(R.string.not_supported) : getString(R.string.support));
                activityWatchInfoBinding.functionList.setText(eaBleWatchInfo.getProj_settings() == 1 ? getString(R.string.support) : getString(R.string.not_supported));
                activityWatchInfoBinding.screenWidth.setText(eaBleWatchInfo.getLcd_full_w() + "");
                activityWatchInfoBinding.screenHeight.setText(eaBleWatchInfo.getLcd_full_h() + "");
                activityWatchInfoBinding.screenType.setText(eaBleWatchInfo.getLcd_full_type() == 0 ? getString(R.string.square_screen) : getString(R.string.circular_screen));
                activityWatchInfoBinding.previewWidth.setText(eaBleWatchInfo.getLcd_preview_w() + "");
                activityWatchInfoBinding.previewHeight.setText(eaBleWatchInfo.getLcd_preview_h() + "");
                activityWatchInfoBinding.thumbnailFillet.setText(eaBleWatchInfo.getLcd_preview_radius() + "");
                activityWatchInfoBinding.snSupported.setText(eaBleWatchInfo.getNot_support_sn() == 0 ? getString(R.string.support) : getString(R.string.not_supported));
                activityWatchInfoBinding.dialSize.setText(eaBleWatchInfo.getMax_watch_size() <= 0 ? "-KB" : eaBleWatchInfo.getMax_watch_size() + "KB");

            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(WatchInfoActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityWatchInfoBinding activityWatchInfoBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityWatchInfoBinding = ActivityWatchInfoBinding.inflate(getLayoutInflater());
        setContentView(activityWatchInfoBinding.getRoot());
        activityWatchInfoBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityWatchInfoBinding.tool);
        getSupportActionBar().setTitle(null);
        activityWatchInfoBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(WatchInfoActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.watch_info, new WatchInfoCallback() {
                @Override
                public void watchInfo(EABleWatchInfo eaBleWatchInfo) {
                    Log.e(TAG, "手表信息:" + eaBleWatchInfo.getIs_wait_for_binding());
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = eaBleWatchInfo;
                        mHandler.sendMessage(message);
                    }

                }

                @Override
                public void mutualFail(int errorCode) {
                    Log.e(TAG, "获取手表信息失败:" + errorCode);
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }

                }
            });
        }

    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
